import Vue from 'vue'
import Router from 'vue-router'
import cookie from '../assets/js/cookie'



const Teacher_Tabbar = () =>import('@/components/Teacher_Tabbar');// /* webpackChunkName: "Tabbar" */
const Student_Tabbar = () =>import('@/components/Student_Tabbar');// /* webpackChunkName: "Tabbar" */

const tab_home = () => import( /* webpackChunkName: "tabbar-home" */ '@/pages/home/order.vue');
const tab_kaohedan = () => import(/* webpackChunkName: "tabbar-class" */ '@/pages/home/kaohedan.vue');
const tab_shensu_shenhe = () => import(/* webpackChunkName: "tabbar-class" */ '@/pages/home/shensu_shenhe.vue');
const tab_user_center = () => import(/* webpackChunkName: "tabbar-class" */ '@/pages/home/user_center.vue');

const stu_score = () => import(/* webpackChunkName: "tabbar-class" */ '@/pages/student/stu_score.vue');
const stu_apply = () => import(/* webpackChunkName: "tabbar-class" */ '@/pages/student/stu_apply.vue');

import view_login from '../pages/login/login.vue'
import order_detail from '../pages/order/order_detail.vue'
import check_order_detail from '../pages/order/check_order_detail.vue'
import selectStu from '../pages/order/select_stu.vue'
import select_project from '../pages/order/select_project.vue'

import student_center from '../pages/student/student_center.vue'
import score_detail from '../pages/student/score_detail.vue'
import apply_edit from '../pages/student/apply_edit.vue'

import appeal_apply_detail from '../pages/student/appeal_apply_detail.vue'

import forget_passowrd_code from '../pages/user/forget_passowrd_code.vue'
import forget_password_phone from '../pages/user/forget_password_phone.vue'
import forget_password_set from '../pages/user/forget_password_set.vue'
import student_info from '../pages/user/student_info.vue'
import teacher_info from '../pages/user/teacher_info.vue'



Vue.use(Router)

const router =new Router({
  routes: [
    //登录
    {
        path: "/:schoolNo/login",
        name: "login",
        component:  view_login , 
        meta: {
          title:"第三课堂-登录",
        }
    },
    //首页开单
    {
          path: "/tea_home",
          name: "tea_home",
          components:  { default: tab_home, tabbar: Teacher_Tabbar,}, 
          meta: {
            title:"第三课堂",
          }
      },
    //开单详情
    {
      path: "/order/order_detail",
      name: "order_detail",
      component: order_detail,
      meta: {
        title: "确认开单",
        keepAlive: true
      }
    },
    //开单详情
    {
      path: "/order/check_order_detail",
      name: "check_order_detail",
      component: check_order_detail,
      meta: {
        title:"考核单详情"
      }
    },
    //开单详情
    {
      path: "/appeal/appeal_apply_detail",
      name: "appeal_apply_detail",
      component: appeal_apply_detail,
      meta: {
        title:"申诉单详情"
      }
    },
    //选择学生
    {
      path: "/order/selectStu",
      name: "selectStu",
      component: selectStu,
      meta: {
        title:"选择学生",
      }
    },
    //选择项目
    {
      path: "/order/select_project",
      name: "selectProject",
      component: select_project,
      meta: {
        title:"选择考核项目",
      }
    },
    //重置密码 - 输入验证码
    {
      path: "/order/forget_passowrd_code",
      name: "forgetPasswordCode",
      component: forget_passowrd_code,
      meta: {
        title:"重置密码",
      }
    },
    //重置密码 - 输入电话
    {
      path: "/order/forget_password_phone",
      name: "forgetPasswordPhone",
      component: forget_password_phone,
      meta: {
        title:"重置密码",
      }
    },
    //学籍信息
    {
      path: "/user/student_info",
      name: "student_info",
      component: student_info,
      meta: {
        title:"学籍信息",
      }
    },
    //个人信息
    {
      path: "/user/teacher_info",
      name: "teacher_info",
      component: teacher_info,
      meta: {
        title:"个人信息",
      }
    },
    //重置密码 - 输入新密码
    {
      path: "/order/forget_password_set",
      name: "forgetPasswordSet",
      component: forget_password_set,
      meta: {
        title:"重置密码",
      }
    },
     {
        path: "/tab_kaohedan",
        name: "tab_kaohedan",
        components: { default: tab_kaohedan, tabbar: Teacher_Tabbar,},
        meta: {
          title:"第三课堂-考核单",
        }
    },
    {
        path: "/shensu_shenhe",
        name: "shensu_shenhe",
      
        components: { default: tab_shensu_shenhe, tabbar: Teacher_Tabbar,},
        meta: {
          title:"第三课堂-申诉审核",
        }
      },

      
     {
        path: "/stu_score",
        name: "stu_score",
        components: { default: stu_score, tabbar: Student_Tabbar,},
        meta: {
          title:"第三课堂-我的成绩",
        }
    },
       {
        path: "/score_detail",
        name: "score_detail",
      
        component: score_detail,
        meta: {
          title:"成绩明细",
        }
      },
    {
        path: "/stu_apply",
        name: "stu_apply",
      
        components: { default: stu_apply, tabbar: Student_Tabbar,},
        meta: {
          title:"第三课堂-扣分申诉",
        }
      },
    {
        path: "/apply_edit",
        name: "apply_edit",
        component: apply_edit,
        meta: {
          title:"申诉",
        }
      },
    {
        path: "/user_center",
        name: "user_center",
        components: { default: tab_user_center, tabbar: Teacher_Tabbar,},
        meta: {
          title:"第三课堂-个人中心",
        }
      },
    {
        path: "/stu_center",
        name: "stu_center",
        components: { default: student_center, tabbar: Student_Tabbar,},
        meta: {
          title:"第三课堂-个人中心",
        }
      },
    {
        path: "/stu_open_center",
        name: "stu_open_center",
        components: {
          default: student_center,
          tabbar: Teacher_Tabbar,
        },
        meta: {
          title:"第三课堂-个人中心",
        }
      },
  ]
})

router.beforeEach((to, from, next) => {
  console.log(location.href);
 // console.log(to);
  if (to.meta && to.meta.title) {
    document.title = to.meta.title;
  }
  var schoolNo = cookie.get(cookie.key.schoolNo);
  var userId = cookie.getUserId();
  if (to.name == 'login'){//如果目标是登录页
    next();
  } else if (userId && schoolNo && !to.query.tsSchoolNo) { //数据库里面又 schoolNo 并且 没有tsSchoolNo
     to.query.tsSchoolNo = schoolNo;
     next({
       path: to.path,
       query: to.query
     });
  } else if (userId && schoolNo && schoolNo == to.query.tsSchoolNo) {
    next();
  }else{
    var start = location.href.split("3rdclassroom/#");
    var schoolNo = "";
    if (to.query.tsSchoolNo) {
      schoolNo = to.query.tsSchoolNo;
    } else if (from.query.tsSchoolNo) {
      schoolNo = from.query.tsSchoolNo;
    }
    if (schoolNo){
      var url = start[0] + "3rdclassroom/#/" + schoolNo + "/login";
      alert("登录信息失效,重新登录");
      next({
         path: schoolNo + "/login",
         params: {
           schoolNo: schoolNo
         }
      });
    }else{
      alert("请重新进入");
    }
  }
});

export default router
